package com.houdoudou.customer.me;




import com.houdoudou.customer.R;
import com.houdoudou.customer.common.Constant;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class NetConfigActivity extends Activity{
	
	
	Button confirm;
    EditText ip;
    View.OnClickListener myClickListener;
    TextView hint;
       @Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
	
    	   super.onCreate(savedInstanceState);
    	   setContentView(R.layout.act_netconfig);
    	   init_listener();
    	   confirm =(Button) findViewById(R.id.btn_confirm_act_netconfig);
   	    	ip=(EditText) findViewById(R.id.et_ip_act_netconfig);
   		   confirm.setOnClickListener(myClickListener);
   		   hint= (TextView) findViewById(R.id.tv_hint_act_netconfig);
   		   confirm.setOnClickListener(myClickListener);
	}

	
       

	private void init_listener(){
		myClickListener=new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			if(v==confirm){
				if(check_ip_valid(ip.getText().toString())){
					{	
					     SharedPreferences sharedPreferences = NetConfigActivity.this.getSharedPreferences("config", Context.MODE_PRIVATE);
						  Editor editor = sharedPreferences.edit();   
						  editor.putString("BaseUrl", "http://"+ip.getText().toString());
						 if(  editor.commit()){
							 
							 Constant.BaseUrl="http://"+ip.getText().toString();
						     hint.setText("IP修改成功");
						 } 
					}
				}
			}
				
			}
		};
		
	}
	
	private boolean check_ip_valid(String ip){
	 boolean result=true;	
		if(!ip.contains(".")) result=false;
		String[] div=ip.split("\\.");
		if(div.length!=4) result=false;
		else{
			
			try{for(int i=0;i<div.length;i++){
		   
			int temp= Integer.parseInt(div[i]);
			if(temp>255||temp<0) {result=false;
			break;}
		
			}
			}catch(Exception e){
				e.printStackTrace();
				result=false;
			}
		}
		
	 if(!result)  hint.setText("IP地址不规范,请重新输入");
	 return result;
	}
	

}
